package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.example.myapplication.adapter.SearchHotAdapter;
import com.example.myapplication.api.MusicApi;
import com.example.myapplication.entity.BaseResponse;
import com.example.myapplication.entity.Hots;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchActivity extends AppCompatActivity {
    private RecyclerView recyclerViewHot;
    private RecyclerView recyclerViewArea;
    private Context context;
    private SearchHotAdapter hotAdapter;
    private List<Hots> hotsList = new ArrayList<>();
    private EditText edtKeyWord;
    private String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        context = this;

        edtKeyWord = findViewById(R.id.edt_search);
        recyclerViewHot = findViewById(R.id.recycler_view_hot);
        GridLayoutManager manager1 = new GridLayoutManager(context, 2);

        recyclerViewHot.setLayoutManager(manager1);
        hotAdapter = new SearchHotAdapter(context);
        recyclerViewHot.setAdapter(hotAdapter);

        loadDataHot();

        edtKeyWord.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    ((InputMethodManager) edtKeyWord.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            SearchActivity.this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    text = edtKeyWord.getText().toString();
                    Intent intent = new Intent(context, ResultActivity.class);
                    intent.putExtra("search_input", text);
                    startActivity(intent);
                    return true;
                }
                return false;

            }
        });

        hotAdapter.setBaseOnclickListener(new BaseOnclickListener() {
            @Override
            public void click(int position) {
                Intent intent = new Intent(context, ResultActivity.class);
                intent.putExtra("search_key", hotsList.get(position));
                startActivity(intent);
            }
        });


    }

    private void loadDataHot(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        MusicApi api = retrofit.create(MusicApi.class);
        Call<BaseResponse> call = api.getSearchHot();
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                BaseResponse baseResponse = response.body();
                if (baseResponse.isSuccess()){
                    hotsList = baseResponse.getData();
                    hotAdapter.setData(hotsList);
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}